<template>
    <view>
        <view style="width: 100%; height: 100vh">
            <checkbox-group @change="checkboxChange">
                <label class="item" v-for="(item, index) in weeks" :key="index">
                    <view>{{ item.name }}</view>

                    <view>
                        <checkbox :value="item.value" :checked="item.checked" />
                    </view>
                </label>
            </checkbox-group>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            parseCron: getApp().globalData.utils.parseCron,
            weeks: [
                {
                    value: 0,
                    name: '周日',
                    checked: false
                },
                {
                    value: 1,
                    name: '周一',
                    checked: false
                },
                {
                    value: 2,
                    name: '周二',
                    checked: false
                },
                {
                    value: 3,
                    name: '周三',
                    checked: false
                },
                {
                    value: 4,
                    name: '周四',
                    checked: false
                },
                {
                    value: 5,
                    name: '周五',
                    checked: false
                },
                {
                    value: 6,
                    name: '周六',
                    checked: false
                }
            ]
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        console.log('=== scene:condition-timer-week:onLoad:');
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log('=== scene:condition-timer-week:onReady:');
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('=== scene:condition-timer-week:onShow:');
        let conditionList = uni.getStorageSync('scene').conditionList; // getApp().globalData.scene.conditionList

        let fields = this.parseCron(conditionList);
        let dayOfWeek = fields.dayOfWeek;
        let weeks = this.weeks;

        for (let i = 0; i < weeks.length; i++) {
            const item = weeks[i];
            item.checked = false;

            for (let j = 0; j < dayOfWeek.length; j++) {
                const pro = dayOfWeek[j];

                if (item.value === pro) {
                    item.checked = true;
                    break;
                }
            }
        }
        this.weeks = weeks
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        console.log('--- scene:condition-timer-week:onHide:');
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console.log('--- scene:condition-timer-week:onUnload:');
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        checkboxChange(ev) {
            console.log(ev, this.weeks);
            const value = ev.detail.value; // 限制条件

            if (value.length < 1) {
                uni.showToast({
                    title: '至少选择一个日期',
					icon:'none',
                    success: () => {
                        // 回设
                    }
                });
                return;
            } // 操作后收集值

            const weeks = this.weeks;
            const valueArray = [];

            for (let i = 0; i < value.length; i++) {
                const item = Number(value[i]);
                valueArray.push(item);
            } // 修改缓存
            // let scene = wx.getStorageSync('scene')

            let conditionList = uni.getStorageSync('scene').conditionList; // getApp().globalData.scene.conditionList

            let expression = '';

            for (let i = 0; i < conditionList.length; i++) {
                const item = conditionList[i];

                if (item.conditionType === 1) {
                    expression = item.conditionValue.cron;
                    let expressionArray = expression.split(' ');
                    expressionArray[5] = valueArray.join(',');
                    let newExpression = expressionArray.join(' ');
                    item.conditionValue.cron = newExpression;
                    break;
                }
            } // scene.conditionList = conditionList
            // wx.setStorageSync('scene', scene)

            getApp().syncScene(this, {
                conditionList: conditionList
            }); // 操作后界面展示的实现

            for (let i = 0; i < weeks.length; i++) {
                const item = weeks[i];

                for (let j = 0; j < valueArray.length; j++) {
                    const pro = valueArray[j];

                    if (item.value === pro) {
                        item.checked = true;
                        break;
                    } else {
                        item.checked = false;
                    }
                }
            }
			this.weeks = weeks
        }
    }
};
</script>
<style>
.item {
    display: flex;
    justify-content: space-between;
    padding: 20rpx;
    background: #ffffff;
    border-bottom: 1px solid #eeeeee;
}
.item:hover {
    background: #eeeeee;
}
</style>
